package com.pingancar.creditmanage.action;

import com.pingancar.creditmanage.action.auth.AdminAuthPassport;
import com.pingancar.creditmanage.pojo.AdminPojo;
import com.pingancar.creditmanage.pojo.PaservicePojo;
import com.pingancar.creditmanage.pojo.ShopinfoPojo;
import com.pingancar.creditmanage.service.AdminServiceImpl;
import com.pingancar.creditmanage.service.QueryService;
import com.pingancar.creditmanage.util.HttpHelper;
import com.pingancar.creditmanage.util.JSONReceiver;
import com.pingancar.creditmanage.util.LBSMessage;
import com.pingancar.creditmanage.util.myenum.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2014/9/9 0009.
 */
@Controller
@RequestMapping(value="/adm")
@AdminAuthPassport
public class AdminAction  {
    @Autowired
    private AdminServiceImpl adminService;
    @Autowired
    private QueryService queryService;

    public static final String DEFAULT_ACTION = "/adm/loginPage";

    @RequestMapping("/")
    public String defaultPage() { return "admin/main"; }

    @AdminAuthPassport(validate = false)
    @RequestMapping("/loginPage")
    public String loginPage() {
        return "admin/login";
    }

    @AdminAuthPassport(validate = false)
    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        AdminPojo adminPojo = new AdminPojo();
        adminPojo.setUsername(username);
        adminPojo.setPasswd(password);
        adminPojo = adminService.login(adminPojo);
        if(adminPojo!=null){
            session.setAttribute("admin", adminPojo);
            session.setAttribute("type", UserType.Admin);
            return "redirect:/adm/";
        } else {
            return "redirect:/adm/loginPage";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.setAttribute("type", UserType.Reader);
        session.removeAttribute("admin");
        return "redirect:/adm/";
    }

    @RequestMapping("/pwPage")
    public String pwPage() { return "admin/modifypw"; }

    @RequestMapping("/feedbacklist")
    public String feedbackList() { return "admin/feedbacklist"; }

    @RequestMapping("/cashlist")
    public String cashlist() {
        return "admin/cashlist";
    }

    @RequestMapping("/confirmCash")
    public String confirmCash(HttpSession session, Integer id) {
        AdminPojo adminPojo = (AdminPojo)session.getAttribute("admin");
        if (adminService.confirmCash(adminPojo, id)) {
            return "redirect:./cashlist";
        } else {
            return "admin/error";
        }

    }

    @RequestMapping("/shoplist")
    public String shoplist() {
        return "admin/shoplist";
    }

    @RequestMapping("/addshopPage")
    public String addshopPage(ModelMap modelMap) {
        return "admin/addshop";
    }

    @RequestMapping("/addshop")
    public String addshop(String name, String username, String description, String con, String location,
                          String bh, Double xpos, Double ypos) {
        if (adminService.addShop(name, username, description, con, location, bh, xpos, ypos))
            return "redirect:./shoplist";
        else
            return "admin/error";
    }

    @RequestMapping("/modifyPw")
    public String modifyPassword(HttpSession session, String oldpw, String newpw) {
        AdminPojo adminPojo = (AdminPojo)session.getAttribute("admin");
        if (adminPojo == null || !adminService.modifyPassword(adminPojo, oldpw, newpw))
            return "admin/error";
        return "admin/main";
    }

    @RequestMapping("/shopdetail")
    public String shopdetail(Integer id, ModelMap modelMap) {
        if (id == null)
            return "admin/error";
        ShopinfoPojo shopinfoPojo = queryService.getShopinfoById(id);
        if (shopinfoPojo == null)
            return  "admin/error";

        List l = queryService.getServiceListByType(id, -1);
        modelMap.addAttribute("shop", shopinfoPojo);
        modelMap.addAttribute("service", l);

        return "admin/shopdetail";
    }

    @RequestMapping("/servicedetail")
    public String servicedetail(Integer id, Integer shop, ModelMap modelMap) {
        PaservicePojo paservicePojo;
        if (id == null) {
            if (shop == null)
                return "admin/error";
            ShopinfoPojo shopinfoPojo = queryService.getShopinfoById(shop);
            if (shopinfoPojo == null)
                return "admin/error";
            paservicePojo = new PaservicePojo();
            paservicePojo.setId(-1);
            paservicePojo.setShopinfo(shopinfoPojo);
        } else {
            paservicePojo = queryService.getServiceById(id);
            if (paservicePojo == null)
                return "admin/error";
        }

        modelMap.addAttribute("service", paservicePojo);
        modelMap.addAttribute("type", queryService.getServicetype());
        return "admin/servicedetail";
    }

    @RequestMapping("/modifyshop")
    public String modifyShop(ShopinfoPojo shopinfoPojo) {
        if (adminService.modifyShop(shopinfoPojo))
            return "redirect:./shoplist";
        else
            return "admin/error";
    }

    @RequestMapping("/modifyservcie")
    public String modifyServcie(Integer id, Integer shopid, String tag1, Integer creditcost, Integer type) {
        PaservicePojo paservicePojo;
        if (id == -1) {
            paservicePojo = adminService.addService(shopid, tag1, creditcost, type);
        } else {
            paservicePojo = adminService.modifyService(id, tag1, creditcost, type);
        }

        if (paservicePojo == null)
            return "admin/error";
        else
            return "redirect:./shopdetail?id="+paservicePojo.getShopinfo().getId();
    }
}
